package kontroler;


import interfejs.MainWindowFrame;
import interfejs.OwnersPanel;
import interfejs.PartsPanel;
import interfejs.VehiclesPanel;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.convert.AnnotationStrategy;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.strategy.Strategy;
import org.xml.sax.SAXException;

import Model.Baza;

public class MainWindowController extends Controller implements ActionListener, ChangeListener {
	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser chooser;
		int returnVal;
		
		
		FileNameExtensionFilter filter;
		switch(e.getActionCommand()){
		case "SAVE_XML":
			Component menu = ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker();
			MainWindowFrame main = (MainWindowFrame)((JFrame) SwingUtilities.getRoot(menu));
			
			chooser = new JFileChooser();
		    filter = new FileNameExtensionFilter(
		        "XML files", "xml");
		    chooser.setFileFilter(filter);
		     returnVal = chooser.showSaveDialog(null);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		    	Serializer serializer = new Persister();
		    	File result = new File(chooser.getSelectedFile().getAbsolutePath());
		    	try {
		    		main.baza.beforeSave();
					serializer.write(main.baza, result);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		    }
			
		break;
		case "LOAD_XML":
			chooser = new JFileChooser();
		    filter = new FileNameExtensionFilter(
		        "XML files", "xml");
		    chooser.setFileFilter(filter);
		     returnVal = chooser.showOpenDialog(null);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		    	Component menu2 = ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker();
				MainWindowFrame main2 = (MainWindowFrame)((JFrame) SwingUtilities.getRoot(menu2));
			
				Strategy strategy = new AnnotationStrategy();
				Serializer serializer = new Persister(strategy);
				
				File source = new File(chooser.getSelectedFile().getAbsolutePath());
				
				Source schemaFile = new StreamSource("schema.xml");
				Source xmlFile = new StreamSource(source);
				
				try {
					
					//Walidacja xml schema
					SchemaFactory schemaFactory = SchemaFactory
					    .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
					Schema schema = schemaFactory.newSchema(schemaFile);
					Validator validator = schema.newValidator();
					validator.validate(xmlFile);
					System.out.println(xmlFile.getSystemId() + " is valid");
					
					
					main2.baza = serializer.read(Baza.class, source);
					main2.baza.mapping(); 
					JOptionPane.showMessageDialog(main2, "Poprawny plik XML, za�adowano!");
					
					
				} catch (SAXException ea) {
					  System.out.println(xmlFile.getSystemId() + " is NOT valid");
					  JOptionPane.showMessageDialog(main2, "Niepoprawny plik xml");
					
				} catch (Exception ex) {
					// TODO Auto-generated catch block
					ex.printStackTrace();
				}
		    }
		break;
		case "EXIT":
			System.exit(0);
		break;
		}

	}


	@Override
	public void stateChanged(ChangeEvent e) {
		JTabbedPane jtabbedPane = (JTabbedPane)e.getSource();
		
		if(jtabbedPane.getSelectedComponent() instanceof OwnersPanel){
			OwnersPanel temp = (OwnersPanel) jtabbedPane.getSelectedComponent();
			temp.getOwnersTableModel().setTable(Baza.getWlascicieleStatic());
			System.out.println("DEBUG !!");
			temp.getOwnersTable().repaint();
			
		}
		
		if(jtabbedPane.getSelectedComponent() instanceof VehiclesPanel){
			VehiclesPanel temp = (VehiclesPanel) jtabbedPane.getSelectedComponent();
			temp.getVehiclesTableModel().setTable(Baza.getPojazdyStatic());
			System.out.println("DEBUG !!");
			temp.getVehiclesTable().repaint();
			
		}
		
		/*if(jtabbedPane.getSelectedComponent() instanceof PartsPanel){
			PartsPanel temp = (PartsPanel) jtabbedPane.getSelectedComponent();
			temp.getPartsTableModel().setTable(Baza.getWarsztatStatic().getCzesci());
			System.out.println("DEBUG !!");
			temp.getPartsTable().repaint();
			
		}
			*/
	}

}


